<?php
##############################################
#
#   会员
# @ Time : 2017-1-8 - 下午9:20:27
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################


namespace Admin\Model;

class UsersModel extends AdminBaseModel{
	/*会员表  */
	protected $tableName = 'users';	
	
	public function __construct($tableName=''){
		//初始化数据库对象
		if(!empty($tableName)){
			$this -> tableName = $tableName;
		}	
		parent::__construct($this -> tableName);
	}
	
	/**
	 * 获取会员
	 */
	public function getUsers($nickname, $phone_number, $email, $category){
	 	if($nickname){
			$map['nickname'] = array('like','%'.$nickname.'%');
		}
		if($phone_number){
			$map['phone_number'] = array('like','%'.$phone_number.'%');
		} 
		if($email){
		    $map['email'] = array('like','%'.$email.'%');
		}
		if($category){
		    $map['category'] = $category;
		}
	    $count = $this -> where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, C('listRows'));
	    $show = $Page->show();
	    $result = $this->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		return array($result, $show);
	}
	
	/**
	 * 获取一个会员
	 */
	public function getUsersFind($id){
	    return $this->where("id = $id")->find();
	}
	
	/**
	 * 修改会员
	 */
	public function editUsers($id, $data){
	    return $this->where("id = $id")->save($data);
	    
	}
	
	/**
	* 删除用户
	*/
	public function deleteUsers($id){
		return $this->where("id = $id")->delete();
	}
    
	/**
	 * 全选删除会员
	 */
	public function deleteAllUsers($idArr) {
	    foreach ($idArr['users_id'] as $k => $v){
	        $this->where("id = $v")->delete();
	    }
	    return true;
	}
	
	/**
	  * 统计会员
	  * @param unknown $today
	  * @date 2017年8月8日 - 下午3:43:09
	  * @author JiangYongWei
	 */
	public function getUserCount($today){
	    $count['all_user'] = $this->count();
	    $count['new_user'] = $this->where("reg_time > $today")->count();
	    return $count;
	}
	
	
	
}